<?php

namespace App\Http\Controllers;

use Filament\Facades\Filament;
use Illuminate\Http\Request;
use Mockery\Matcher\Not;
use Yansongda\LaravelPay\Facades\Pay;
use App\Models\Order;
use Filament\Notifications\Notification;
use App\Models\User;
use App\Models\Balance;
use Illuminate\Support\Facades\Log;

class PaymentController extends Controller
{
     function alipay(Request $request)
     {
         $price = $request->price;
         $plan_name = $request->plan_name;
         $order_id = $request->order_id;
         $order = [
             'out_trade_no' => $order_id,
             'total_amount' => $price,
             'subject' => '购买' . $plan_name,
         ];
         return Pay::alipay()->web($order);
     }

    /**
     * @param Request $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Yansongda\Artful\Exception\ContainerException
     * @throws \Yansongda\Artful\Exception\InvalidParamsException
     */

    /**
     * @param Request $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Yansongda\Artful\Exception\ContainerException
     * @throws \Yansongda\Artful\Exception\InvalidParamsException
     * two types of order, recharge balance and buy plan, check if the order is recharge balance
     */
     function alipay_callback(Request $request)
     {
          $results = $request->json()->all();
          $payment_id = $results["trade_no"];
          $paid_at = $results["gmt_payment"];
          $out_trade_no = $results["out_trade_no"];
          $order_id = Order::where('order_id', $out_trade_no)->first()->id;
          $order_long_id = Order::where('order_id', $out_trade_no)->first()->order_id;
          $order_type = Order::find($order_id)->order_type;
         $plan_id = Order::find($order_id)->plan_id;
         $user_id = Order::find($order_id)->user_id;
         $referral_id = User::find($user_id)->referred_by;
         updateOrderAfterPayment($order_id, $payment_id, $paid_at);
         Notification::make("OrderPaid")
             ->title("您的订单".$order_long_id."已收到支付！")
             ->success()
             ->sendToDatabase(User::find($user_id));
         if($order_type == 'balance_recharge'){
              $this->updateBalanceAfterPayment($order_id);
         }

         if($order_id && $plan_id && $referral_id){
             updateReferralCommission($plan_id, $order_id, $referral_id);
         }

         //return Pay::alipay()->success();
     }



     function alipay_return(Request $request)
     {
         return redirect()->route('home');
     }

    /**
     * @param $trade_no
     * @param $payment_id
     * @return void
     * update order status, update user balance
     */

     function updateBalanceAfterPayment($order_id)
     {
         $amount = Order::find($order_id)->amount;
         $user_id = Order::find($order_id)->user_id;
         $balance_before = User::find($user_id)->balance;
         $balance_after = $balance_before + $amount;
         User::find($user_id)->update(['balance' => $balance_after]);
            $balance = new Balance();
            $balance->user_id = $user_id;
            $balance->order_id = $order_id;
            $balance->balance_before = $balance_before;
            $balance->balance_after = $balance_after;
            $balance->type = "deposit";
            $balance->save();
     }
}
